package com4280;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * This class is a repository for a lot of the shared fields
 * and methods that are used by both children to complete their tasks
 * 
 * @author Mitchell Whitehouse
 * @author Andrew Evans	
 * @author Duncan Grant
 * @version     1                                 
 * @since       1.6                                      
 */
public abstract class SearchEngine {
	
	public final String OUTPUT_DIRECTORY = "Output" + File.separator + "Indexer";
	public File dirs = new File(OUTPUT_DIRECTORY);
	public final String INPUT_FILENAME = "Output/Spider/localIWURLs.txt";
	public final String INPUT_DIRECTORY = "Output" + File.separator + "Spider" + File.separator;
	public final String STOPWORDS_FILENAME = "stopWords.txt";
	public final String OUTPUT_FILENAME = "Output/Indexer/index.txt";
	public ArrayList<String> URLs = new ArrayList<String>();
	public ArrayList<String> stopWords = loadFile(STOPWORDS_FILENAME);
	public HashMap<String, ArrayList<String>> index = new HashMap<String,ArrayList<String>>();
	public SearchGUI gui;
	
	/**
	 * 
	 * A simple file loader
	 * 
	 * A generic file loader that loads the information from
	 * the given file name into an arraylist, saving each line as a
	 * new item.
	 * @param al Container to hold contents of file
	 * @param filename Name of file to open
	 */
	//Generic file loader, saves data in arraylist
	
	
	public ArrayList<String> loadFile(String filename){
		ArrayList<String> al = new ArrayList<String>();
		try{
			FileInputStream fstream = new FileInputStream(filename);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			//Read File Line By Line
			while ((strLine = br.readLine()) != null)   {
				// Print the content on the console
				al.add(new String(strLine.trim()));
			}
			//Close the input stream
			in.close();
		}catch (IOException e){//Catch exception if any
			System.err.println("Search Engine Error - " + filename + ": " + e);
			System.out.println("INPUT DIRECTORY: " + INPUT_DIRECTORY);
		}
		return al;
	}

	
}
